package com.hdax.dm.controller;

import com.hdax.dm.common.exception.user.UserLoginException;
import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.entity.item.Item;
import com.hdax.dm.service.ItemCService;
import com.hdax.dm.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
public class ItemCController {

    @Autowired
    private ItemCService itemCService;

    @PostMapping(path = "/recommend")           //查询首页今日推荐
    public CommonResponse<List<RecommendVo>> recommend(){
        return DtoUtil.returnSuccess("",itemCService.recommend());
    }

    @PostMapping(path = "/sell")           //查询首页即将开售
    public CommonResponse<List<RecommendVo>> sell(){
        return DtoUtil.returnSuccess("",itemCService.sell());
    }

    @PostMapping(path = "/seniority")           //查询主页楼层热门演出
    public CommonResponse<List<seniorityVo>> seniority(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.seniority(params.get("itemTypeId")));
    }

    @PostMapping(path = "/guesslike")           //搜索页猜你喜欢查询
    public CommonResponse<List<guesslikeVo>> guesslike(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.guesslike(params.get("itemTypeId")));
    }

    @PostMapping(path = "/getComments")           //商品详情评价查询
    public CommonResponse<List<getCommentsVo>> getComments(@RequestBody Map<String,String> params, HttpServletRequest request){
        Object object = new Object();
        if (params.size() == 1){
            object = itemCService.getComments(Long.parseLong(params.get("id")));
        } else {
            //获取用户id
            String token = request.getHeader("token");
            if (token == null || token.equals("")){
                throw new UserLoginException(UserLoginException.ERR_INFO.NO_LOGIN_MSG,
                        UserLoginException.ERR_INFO.NO_LOGIN_CODE);
            }
            String s = token.split(":")[1].split("-")[0];
            params.remove("userId");
            params.put("userId",s);
            object = itemCService.addComments(params);
        }
        return DtoUtil.returnSuccess("",object);
    }

    @PostMapping(path = "/getRecommend")           //商品详情页热门推荐查询
    public CommonResponse<List<getRecommendVo>> getRecommend(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.getRecommend(params.get("itemTypeId")));
    }

    @PostMapping(path = "/addComments")           //添加评论
    public CommonResponse<List<getRecommendVo>> addComments(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.getRecommend(params.get("itemTypeId")));
    }

    @PostMapping(path = "/getGuessYouLike")           //不同分类页猜你喜欢查询
    public CommonResponse<List<guesslikeVo>> getGuessYouLike(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.guesslike(params.get("itemTypeId")));
    }

    @PostMapping(path = "/getCarouselData")           //不同分类页轮播图查询
    public CommonResponse<List<CarouselDto>> getCarouselData(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.getCarouselData(params.get("itemTypeId")));
    }

    @PostMapping(path = "/getAdimgUrl")           //不同分类三个图查询
    public CommonResponse<List<getRecommendVo>> getAdimgUrl(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.getAdimgUrl(params.get("itemTypeId")));
    }

    @PostMapping(path = "/getWonderfulData")           //不同分类精彩聚集查询
    public CommonResponse<List<getRecommendVo>> getWonderfulData(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.getWonderfulData(params.get("itemTypeId")));
    }

    @PostMapping(path = "/getPopularSearch")           //不同分类热门排行查询
    public CommonResponse<List<getRecommendVo>> getPopularSearch(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.getPopularSearch(params.get("itemTypeId")));
    }

    @PostMapping(path = "/getParentChild")           //不同分类年龄段商品查询
    public CommonResponse<List<getRecommendVo>> getParentChild(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.getParentChild(params));
    }

    @PostMapping(path = "/getCalendarData")           //分类页面演出月份票查询
    public CommonResponse<List<getCalendarDataVo>> getCalendarData(@RequestBody Map<String,Long> params){
        return DtoUtil.returnSuccess("",itemCService.getCalendarData(params.get("itemTypeId")));
    }


}
